package com.woniuxy.conf;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author: 曾令忠
 * @Description: 此配置文件的作用：1.创建消息队列和交换机；2.绑定消息队列和交换机
 * @Date: 2020/7/4 0004-下午 3:55
 */
@Configuration
public class RabbitMqConf {

    @Bean
    public Queue queue(){
        return  new Queue("hello"); //消息队列的名字
    }

    /**
     * 订阅模式
     */
    @Bean
    public Queue fanoutQueueA(){
        return  new Queue("fanout_a");
    }
    @Bean
    public Queue fanoutQueueB(){
        return  new Queue("fanout_b");
    }

    //交换机
    @Bean
    public FanoutExchange fanoutExchange(){
        return  new FanoutExchange("fanoutExchange");
    }

    //绑定交换机和消息队列，目的是当交换机接受到消息后转发给对应的队列
    @Bean
    public Binding bindingA(FanoutExchange fanoutExchange,Queue fanoutQueueA){
        return BindingBuilder.bind(fanoutQueueA).to(fanoutExchange);
    }
    @Bean
    public Binding bindingB(FanoutExchange fanoutExchange,Queue fanoutQueueB){
        return BindingBuilder.bind(fanoutQueueB).to(fanoutExchange);
    }

    /**
     * 路由模式的消息队列
     */
    @Bean
    public Queue routingQueueA(){
        return  new Queue("routing_a");
    }

    /**
     * 路由模式的消息队列
     */
    @Bean
    public Queue routingQueueB(){
        return  new Queue("routing_b");
    }

    /**
     * 路由模式的交换机
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("directExchange");
    }

    /**
     * 绑定交换机和队列
     */
    @Bean
    public Binding bindingDirectAndA(DirectExchange directExchange,Queue routingQueueA){
        return BindingBuilder.bind(routingQueueA).to(directExchange).with("news");
    }
    /**
     * 绑定交换机和队列
     */
    @Bean
    public Binding bindingDirectAndB(DirectExchange directExchange,Queue routingQueueB){
        return BindingBuilder.bind(routingQueueB).to(directExchange).with("tech");
    }

    /**
     * 主题模式
     */
    @Bean
    public  Queue topicQueueA(){
        return  new Queue("topicQueueA");
    }
    /**
     * 主题模式
     */
    @Bean
    public  Queue topicQueueB(){
        return  new Queue("topicQueueB");
    }

    /**
     * 主题模式
     */
    @Bean
    public  TopicExchange topicExchange(){
        return  new TopicExchange("topicExchange");
    }

    /**
     * 绑定交换机和队列
     */
    @Bean
    public Binding bindingTopicA(TopicExchange topicExchange,Queue topicQueueA){
        return BindingBuilder.bind(topicQueueA).to(topicExchange).with("#.news");
    }
    /**
     * 绑定交换机和队列
     */
    @Bean
    public Binding bindingTopicB(TopicExchange topicExchange,Queue topicQueueB){
        return BindingBuilder.bind(topicQueueB).to(topicExchange).with("chengdu.#");
    }

    /**
     * 应用解耦
     */
    @Bean
    public Queue rpcQueueA(){
        //存放队列初始化参数
        Map<String,Object> args = new HashMap<>();
        //指定消息队列的长度
        args.put("x-max-length",10);
        //指定溢出消息队列的消息，拒绝接收
        args.put("x-overflow","reject-publish");
        //参数1：队列名；参数2：是否持久化消息队列，如果让rabbitmq重启队列还在；参数3：是否为排他队列，是否只有自己可见；
        //参数4：是否在不用改消息队列时参数队列；参数5：初始化参数
        return  new Queue("rpc_queue_a",true,false,false,args);
    }
    @Bean
    public FanoutExchange rpcChangeA(){
        return new FanoutExchange("rpc_exchange_a");
    }
    @Bean
    public Binding bindingrpcQueueAandrpcChangeA(Queue rpcQueueA,FanoutExchange rpcChangeA){
        return BindingBuilder.bind(rpcQueueA).to(rpcChangeA);
    }

}
